package VIEWS;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

import ModelEntityFW.UserEntitys;
import Ultils.SQLConnections;

public class frmMainApp extends JFrame {

    private final JPanel contentPane;
    private static frmflashScreen frm;
    private static JButton btnDangNhap;
    private UserEntitys m_objSecurityContext;
    private static Connection m_objConnection = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    frm = new frmflashScreen();
                    frm.setLocation(300, 200);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        SQLConnections objConnObject = new SQLConnections();
        try {
            m_objConnection = objConnObject.OpenConections("192.168.100.200", "Y2015JC60_PROJECTS", "y2015jc60",
                    "123456", 1433);
        } catch (Exception e) {
            e.printStackTrace();
        }
        UserEntitys objSecurityContexts = new UserEntitys();
        LoadForm(m_objConnection, objSecurityContexts);
    }

    static void LoadForm(Connection objConnection, UserEntitys objSecurity) {
        try {
            frm.setVisible(true);
            Thread.sleep(1000);
            frm.dispose();

            frmLogin frm = new frmLogin(objConnection, objSecurity);
            frm.setLocation(400, 300);
            frm.setVisible(true);
        } catch (InterruptedException e) {
        }
    }

    public void SetLoginInfo(UserEntitys objSecurity) {
        if (objSecurity.isLoginSuccess()) {
            m_objSecurityContext = objSecurity;
            btnDangNhap.setText("[" + objSecurity.getFullName() + "]");
        } else {
            btnDangNhap.setText("[Đăng Nhập]");
        }
    }

    /**
     * Create the frame.
     */
    public frmMainApp() {
        setTitle("PHẦN MỀM QUẢN LÝ BỆNH VIỆN");
        setIconImage(Toolkit.getDefaultToolkit().getImage(
                frmMainApp.class.getResource("/RESOURCES/o_348eb27af0aab1d0-0.png")));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1110, 770);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnHThng = new JMenu("Hệ Thống");
        mnHThng.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/top_work_tree.png")));
        menuBar.add(mnHThng);

        JMenuItem mntmCuHnhH = new JMenuItem("Cấu hình hệ thống");
        mntmCuHnhH.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/action_settings_2.png")));
        mnHThng.add(mntmCuHnhH);

        JMenuItem mntmCuHnhQuyn = new JMenuItem("Cấu hình Quyền");
        mntmCuHnhQuyn.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/2.png")));
        mnHThng.add(mntmCuHnhQuyn);

        JMenuItem mntmThot = new JMenuItem("Thoát");
        mntmThot.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                int Option = JOptionPane.showConfirmDialog(getParent(), "Bạn có chắc chắn muốn thoát không?",
                        "Thông báo", JOptionPane.YES_NO_OPTION);
                if (Option == 0) {
                    dispose();
                }
            }
        });
        mntmThot.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/delete.gif")));
        mnHThng.add(mntmThot);

        JMenu mnDanhMc = new JMenu("DANH MỤC");
        mnDanhMc.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/edit32.png")));
        menuBar.add(mnDanhMc);

        JMenuItem mniDMChucVu = new JMenuItem("DM - Chức vụ");
        mniDMChucVu.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/2.png")));
        mnDanhMc.add(mniDMChucVu);

        JMenuItem mniDMDichVu = new JMenuItem("DM - Dịch vụ");
        mniDMDichVu.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/action_settings.png")));
        mnDanhMc.add(mniDMDichVu);

        JMenuItem mniDMKhoa = new JMenuItem("DM - Khoa");
        mniDMKhoa.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        mniDMKhoa.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/kate32x32.png")));
        mnDanhMc.add(mniDMKhoa);

        JMenuItem mniDMNhaCCT = new JMenuItem("DM - Nhà CC Thuốc");
        mniDMNhaCCT.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/action_settings_2.png")));
        mnDanhMc.add(mniDMNhaCCT);

        JMenuItem mniDMLoaiThuoc = new JMenuItem("DM - Loại thuốc");
        mniDMLoaiThuoc.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/calendar.gif")));
        mnDanhMc.add(mniDMLoaiThuoc);

        JMenuItem mniDMPhongKham = new JMenuItem("DM - Phòng khám");
        mniDMPhongKham.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        mniDMPhongKham.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/save.png")));
        mnDanhMc.add(mniDMPhongKham);

        JMenuItem mniDMPhongDieuTri = new JMenuItem("DM - Phòng điều trị");
        mniDMPhongDieuTri.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/settings_16.png")));
        mnDanhMc.add(mniDMPhongDieuTri);

        JMenuItem mniDMPhanQuyen = new JMenuItem("DM - Phân quyền");
        mniDMPhanQuyen.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/5.png")));
        mnDanhMc.add(mniDMPhanQuyen);

        JMenuItem mniDMThuoc = new JMenuItem("DM - Thuốc");
        mniDMThuoc.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/Go.png")));
        mnDanhMc.add(mniDMThuoc);
        JMenu mnBoCo = new JMenu("BÁO CÁO");
        mnBoCo.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/13.png")));
        menuBar.add(mnBoCo);
        JMenu mnTrGip = new JMenu("Trợ giúp");
        mnTrGip.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/14.png")));
        menuBar.add(mnTrGip);
        JMenuItem mntmTrGipCho = new JMenuItem("Trợ giúp cho bạn");
        mntmTrGipCho.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

            }
        });
        mnTrGip.add(mntmTrGipCho);

        JMenuItem mntmDesignAbout = new JMenuItem("About");
        mntmDesignAbout.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        mnTrGip.add(mntmDesignAbout);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JToolBar toolBar = new JToolBar();
        toolBar.setBounds(0, 0, 2000, 40);
        contentPane.add(toolBar);

        btnDangNhap = new JButton("[Đăng Nhập]");
        btnDangNhap.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (m_objSecurityContext.isLoginSuccess()) {
                    int opt = JOptionPane.showConfirmDialog(getParent(), "Bạn có chắc chắn muốn đăng xuất không?",
                            "Thông  báo", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null);
                    if (opt == 0) {
                        dispose();
                        SQLConnections objConnObject = new SQLConnections();
                        Connection m_objConnection = null;
                        try {
                            m_objConnection = objConnObject.OpenConections("DESKTOP-AGS3AGQ", "PMQUANLYBENHVIEN", "sa",
                                    "123456", 1433);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        UserEntitys objSecurityContexts = new UserEntitys();
                        LoadForm(m_objConnection, objSecurityContexts);
                    }
                }
            }
        });
        btnDangNhap.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnDangNhap.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/8.png")));
        toolBar.add(btnDangNhap);

        JButton btnThanhVien = new JButton("Danh Sách Nhân Viên");
        btnThanhVien.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                frmUsers frmU = new frmUsers(m_objConnection, m_objSecurityContext);
                frmU.setVisible(true);
            }
        });
        btnThanhVien.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnThanhVien.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/login Manager_2.png")));
        toolBar.add(btnThanhVien);
        JButton btnKhachHang = new JButton("Thông tin thành viên");
        btnKhachHang.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/9.png")));
        btnKhachHang.setFont(new Font("Cambria", Font.PLAIN, 14));
        toolBar.add(btnKhachHang);
        JButton btnHangHoa = new JButton("Phân Quyền Người Dùng");
        toolBar.add(btnHangHoa);
        btnHangHoa.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnHangHoa.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/Login Manager32x32.png")));

        JLabel lblHoSoBenhAn = new JLabel("");
        lblHoSoBenhAn.setToolTipText("Hồ Sơ Bệnh Án");
        lblHoSoBenhAn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

            }
        });

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setIcon(new ImageIcon(frmMainApp.class.getResource("/RESOURCES/PMQuanLyBenhVien.png")));
        lblNewLabel.setBounds(400, 84, 521, 527);
        lblNewLabel.setLocation(400, 100);
        contentPane.add(lblNewLabel);

        JLabel lblBHYT = new JLabel("");
        lblBHYT.setToolTipText("BHYT");
        lblBHYT.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblBHYT.setBounds(585, 130, 75, 74);
        contentPane.add(lblBHYT);

        JLabel lblKhoa = new JLabel("");
        lblKhoa.setToolTipText("Khoa");
        lblKhoa.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblKhoa.setBounds(687, 130, 75, 74);
        contentPane.add(lblKhoa);

        JLabel lblPhongKham = new JLabel("");
        lblPhongKham.setToolTipText("Phòng khám");
        lblPhongKham.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblPhongKham.setBounds(772, 170, 81, 74);
        contentPane.add(lblPhongKham);

        JLabel lblDonThuoc = new JLabel("");
        lblDonThuoc.setToolTipText("Đơn thuốc");
        lblDonThuoc.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblDonThuoc.setBounds(807, 245, 81, 68);
        contentPane.add(lblDonThuoc);

        JLabel lblDichVu = new JLabel("");
        lblDichVu.setToolTipText("Dịch vụ");
        lblDichVu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblDichVu.setBounds(807, 320, 87, 91);
        contentPane.add(lblDichVu);

        JLabel lblHoaDon = new JLabel("");
        lblHoaDon.setToolTipText("Hóa đơn");
        lblHoaDon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblHoaDon.setBounds(807, 422, 81, 84);
        contentPane.add(lblHoaDon);

        JLabel lblPDieuTri = new JLabel("");
        lblPDieuTri.setToolTipText("Điều trị");
        lblPDieuTri.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(getParent(), "Điều trị");
            }
        });
        lblPDieuTri.setBounds(716, 501, 81, 74);
        contentPane.add(lblPDieuTri);

        JLabel lblNhanVien = new JLabel("");
        lblNhanVien.setToolTipText("Nhân viên");
        lblNhanVien.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblNhanVien.setBounds(627, 516, 63, 59);
        contentPane.add(lblNhanVien);

        JLabel lblNhaCCDV = new JLabel("");
        lblNhaCCDV.setToolTipText("Nhà CCDV");
        lblNhaCCDV.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(getParent(), "Nhà CCDV");
            }
        });
        lblNhaCCDV.setBounds(533, 501, 75, 74);
        contentPane.add(lblNhaCCDV);

        JLabel lblTienLuong = new JLabel("");
        lblTienLuong.setToolTipText("Tiền lương");
        lblTienLuong.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblTienLuong.setBounds(450, 438, 73, 74);
        contentPane.add(lblTienLuong);

        JLabel lblGTGT = new JLabel("");
        lblGTGT.setToolTipText("GTGT");
        lblGTGT.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(getParent(), "GTGT");
            }
        });
        lblGTGT.setBounds(426, 338, 73, 73);
        contentPane.add(lblGTGT);

        JLabel lblThuoc = new JLabel("");
        lblThuoc.setToolTipText("Thuốc");
        lblThuoc.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JOptionPane.showMessageDialog(getParent(), "Thuốc");
            }
        });
        lblThuoc.setBounds(430, 252, 81, 75);
        contentPane.add(lblThuoc);

        JLabel lblBenhNhan = new JLabel("");
        lblBenhNhan.setToolTipText("Bệnh nhân");
        lblBenhNhan.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        lblBenhNhan.setBounds(603, 286, 114, 125);
        contentPane.add(lblBenhNhan);
        lblHoSoBenhAn.setBounds(448, 170, 114, 59);
        contentPane.add(lblHoSoBenhAn);
    }
}
